package org.blog.controller;

import org.blog.dao.CommentsMapper;
import org.blog.entity.Comments;
import org.blog.entity.param.CommentsParam;
import org.blog.service.CommentsService;
import org.blog.utils.ResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("comments")

public class CommentsController {
    @Autowired
    private CommentsService commentsService;

    @GetMapping("admin/index")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("admin/comment");

        // 根据参数获取comments
        List<Comments> list = commentsService.getComments(new CommentsParam());

        modelAndView.addObject("list", list);
        return modelAndView;
    }

    @GetMapping("delete/{commentsId}")
    public void delete(@PathVariable("commentsId") Integer commentsId, HttpServletRequest request, HttpServletResponse response) throws IOException {
        boolean delete = commentsService.delete(commentsId);
        if (delete) {
            response.sendRedirect("/comments/admin/index");
        } else {
            response.getWriter().println("删除失败");
            response.getWriter().close();
        }
    }


    @PostMapping("search")
    public ModelAndView search( CommentsParam param) {
        ModelAndView modelAndView = new ModelAndView("/admin/comment");
        List<Comments> comments = commentsService.getComments(param);

        modelAndView.addObject("list", comments);
        return modelAndView;
    }
}
